var UIController = require("UIController");

var CoinController = cc.Class({
    extends: cc.Component,

    properties: {
        currentCoins: cc.Integer,

		coinPrefab: cc.Prefab,
    },

	statics: {
		instance: null,
	},
	ctor() {
		CoinController.instance = this;
	},

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    // update (dt) {},

	addCoins (coinsToAdd) {
		this.currentCoins += coinsToAdd;
		this.currentCoins = Math.round(this.currentCoins);

		UIController.instance.updateCoins(this.currentCoins);
	},

	dropCoins (position, value) {
		let prefab = cc.instantiate(this.coinPrefab);
		prefab.getComponent("CoinPickUp").coinAmount = value;
		prefab.setPosition(position.add(cc.v2(20, 20)));

		return prefab;
	},

	spendCoins (coinToSpend) {
		this.currentCoins -= coinToSpend;

		UIController.instance.updateCoins(this.currentCoins);
	},
});

module.exports = CoinController;
